package com.example.wxchatdemo;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.example.wxchatdemo.Files.MainFiles;
import com.example.wxchatdemo.SQLitess.SQLites;
import com.example.wxchatdemo.other.All;
import com.example.wxchatdemo.pojo.WeChat_msgs;
import com.example.wxchatdemo.pojo.Wechatuser;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Main_one extends AppCompatActivity {
    private List<Wechatuser> mData = null;
    private Context mContext;
    private AnimalAdapter mAdapter = null;
    private ListView list_animal;
    private TextView add_img,find_img;
    //
    private SQLiteDatabase db;
    private SQLites sqLites;
    private SQLites sqLites2;
    private SQLiteDatabase db2;
    private StringBuilder sb;
    private int i = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_one);
        mContext = Main_one.this;
        list_animal = (ListView) findViewById(R.id.list_allmsgs);
        //查找和加号按钮
        add_img= (TextView) findViewById(R.id.add_img);
        find_img=(TextView)findViewById(R.id.find_img);
        //创建数据库
        sqLites = new SQLites(mContext, "my.db", null, 1);
        sqLites2 = new SQLites(mContext, "my.db", null, 1);

        //设置点击事件+
        add_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //***
                View views = LayoutInflater.from(mContext).inflate(R.layout.item_pop, null, false);
                Button btn_xixi = (Button) views.findViewById(R.id.btn_xixi);
                Button btn_hehe = (Button) views.findViewById(R.id.btn_hehe);
                //1.构造一个PopupWindow，参数依次是加载的View，宽高
                final PopupWindow popWindow = new PopupWindow(views,
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
                //设置加载动画
                //这些为了点击非PopupWindow区域，PopupWindow会消失的，如果没有下面的
                //代码的话，你会发现，当你把PopupWindow显示出来了，无论你按多少次后退键
                //PopupWindow并不会关闭，而且退不出程序，加上下述代码可以解决这个问题
                popWindow.setTouchable(true);
                popWindow.setTouchInterceptor(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        return false;
                        // 这里如果返回true的话，touch事件将被拦截
                        // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
                    }
                });
                popWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));    //要为popWindow设置一个背景才有效


                //设置popupWindow显示的位置，参数依次是参照View，x轴的偏移量，y轴的偏移量
                popWindow.showAsDropDown(add_img, 0, 0);

                //设置popupWindow里的按钮的事件
                btn_xixi.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Intent intent = new Intent(Main_one.this, MainFiles.class);
                        startActivity(intent);

                        Toast.makeText(Main_one.this, "扫一扫~", Toast.LENGTH_SHORT).show();
                        //点击后就关闭选项卡
                        popWindow.dismiss();
                    }
                });
                btn_hehe.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(Main_one.this, "添加好友~", Toast.LENGTH_SHORT).show();
                        popWindow.dismiss();
                    }
                });

                //****

            }
        });
        //设置点击事件==放大镜
        find_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view ) {


                //参数依次是：表名，强行插入null值得数据列的列名，一行记录的数据
                System.out.println("开始执行数据库操作");


                SQLiteDatabase db = sqLites.getWritableDatabase();
                db.execSQL("DELETE FROM wechat WHERE 1=1");
                System.out.println("删除===了所有数据");



            }
        });


        //消息页面的listview的生成
        mData = new LinkedList<Wechatuser>();


        List<Wechatuser> wechatusers = GetNoW();
        //从本地数据库取消息人列表
        for (int x=0;x<wechatusers.size();x++){
            String anumber = wechatusers.get(x).getAnumber();
            String aspeak = wechatusers.get(x).getAspeak();
            String lasttime = wechatusers.get(x).getLasttime();
            String aname = wechatusers.get(x).getAname();
            mData.add(new Wechatuser(aname, aspeak, anumber,lasttime));

        }
        //初始化会添加管理员微信
        if (wechatusers.size()==0){

            db2 = sqLites2.getWritableDatabase();
            //参数依次是：表名，强行插入null值得数据列的列名，一行记录的数据
            System.out.println("MAin_one==数据库为空，系统为你添加管理员");
            db2.execSQL("INSERT INTO `wechatuser`(`id`, `aName`, `aSpeak`, `aNumber`, `LastTime`, `myid`, `friends`) VALUES (1, '管理员', '程序出问题联系我', 3, 2, 1, 0);");
            db2.execSQL("INSERT INTO `wechatuser`(`id`, `aName`, `aSpeak`, `aNumber`, `LastTime`, `myid`, `friends`) VALUES (2, '帅哥', '滴滴我', 2, 2, 2, 1);");
            mData.add(new Wechatuser("1","管理员", "程序出问题联系我","3",32321313+"","123","3324"));
            mData.add(new Wechatuser("2","帅哥", "滴滴我","2",323213123+"","321","3324"));
             db2.close();
        }


      //  mData.add(new Wechatuser("麦梓维", "明天没早课了?", R.mipmap.ic_icon_cow,3+""));


        mAdapter = new AnimalAdapter((LinkedList<Wechatuser>) mData, mContext);

        list_animal.setAdapter(mAdapter);

///*****给list的每个item设置点击事件
        list_animal.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                Intent intent = new Intent(Main_one.this,speackActivity2.class);
//                     intent.setClass(MainActivity.this,speackActivity.class);
                //传递备注
                Bundle bundle=new Bundle();
                bundle.putString("f_name",mData.get(i).getAname()+mData.get(i).getAnumber());
                //发送对象的id
                bundle.putString("f_id",mData.get(i).getAnumber());
                //我的id
                //管理员id=3；帅哥id=2
                bundle.putString("my_id","1");
                intent.putExtras(bundle);
                System.out.println("=====查看f_id与f_name"+mData.get(i).getAname()+"-----"+mData.get(i).getAnumber());
                startActivity(intent);
                //时间检测

                long startTime = System.currentTimeMillis();
      }
    });

    }
    //无网络情况下，从本地数据库查找信息
    List<Wechatuser> GetNoW(){
        List list=new ArrayList<Wechatuser>();

        SQLiteDatabase dbs2 = sqLites.getReadableDatabase();
        Cursor cursor =  dbs2.rawQuery("SELECT * FROM wechatuser ",null      );
        //查询数据库
        System.out.println(cursor.getCount()+"<==Main_oned的数据库条数");
        if (cursor.getCount()!=0){
            while (cursor.moveToNext())
            {
                Long id = cursor.getLong(cursor.getColumnIndex("id"));
                Long aNumber = cursor.getLong(cursor.getColumnIndex("aNumber"));
                Long LastTime = cursor.getLong(cursor.getColumnIndex("LastTime"));
                Long myid = cursor.getLong(cursor.getColumnIndex("myid"));
                Long friends = cursor.getLong(cursor.getColumnIndex("friends"));

                String aName=cursor.getString(cursor.getColumnIndex("aName"));
                String aSpeak=cursor.getString(cursor.getColumnIndex("aSpeak"));
                list.add(new Wechatuser(id+"",aName,aSpeak,aNumber+"",LastTime+"",myid+"",friends+""));
                System.out.println("=====执行了Main_one==GetNoW方法");

                // list.add(new WeChat_msgs(personid+"",myid+"",youid+"",times+"",contents+"",status));
                // System.out.println(personid+"----"+"||"+youid+"||"+"---"+"||"+myid+"||"+"-------------"+"||"+contents+"||"+"----"+times+"---"+status);
            }
        }

        cursor.close();//查找数据库

        return list;
    }

}
